/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

#include "ipcam.h"

#ifdef __cplusplus
extern "C" {
#endif

static CIpCam			g_oIpCam;
//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    OpenSocket
 * Signature: (L)I
 */
jint
Java_com_livecam_roadjun_Ipc_OpenSocket(JNIEnv* env, jobject thiz, jstring pszServIp, jint port1, jint port2, jstring pszUserName, jstring pszPassword)
{
	int			iRet;
	const char 	*pszIpAddr, *pszUser, *pszPwd;
	
	pszIpAddr = env->GetStringUTFChars(pszServIp, NULL);  
	if (pszIpAddr == NULL)
	{
		return -1;
	}
	
	pszUser = env->GetStringUTFChars(pszUserName, NULL);
	pszPwd = env->GetStringUTFChars(pszPassword, NULL);

	LOGI("Java_com_livecam_roadjun_Ipc_OpenSocket(ip %s, port1 %d, port2 %d, user %s, pwd %s) begin...\n", pszIpAddr, port1, port2, pszUser, pszPwd); 
	iRet = g_oIpCam.OnLogin(pszIpAddr, port1, port2, pszUser, pszPwd);

	env->ReleaseStringUTFChars(pszPassword, pszPwd);
	env->ReleaseStringUTFChars(pszUserName, pszUser);
	env->ReleaseStringUTFChars(pszServIp, pszIpAddr);
	
	return iRet;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    CloseSocket
 * Signature: ()I
 */
jint
Java_com_livecam_roadjun_Ipc_CloseSocket(JNIEnv* env, jobject thiz)
{
	LOGI("Java_com_livecam_roadjun_Ipc_CloseSocket().\n");
	return g_oIpCam.OnLogout();
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    RegisterCallBack
 * Signature: ()I
 */
static JNIEnv *s_poCmdEnv = NULL;
jint 
Java_com_livecam_roadjun_Ipc_RegisterCallBack(JNIEnv *env, jobject obj, jobject cb)
{
    g_oIpCam.m_pCmdEnv 			= env;
    g_oIpCam.m_objCmdCallBack 	= env->NewGlobalRef(obj);
    g_oIpCam.m_objCmdInterface	= env->NewGlobalRef(cb);
#if 0  
    g_oIpCam.m_pMdaEnv 			= env;
    g_oIpCam.m_objMdaCallBack 	= env->NewGlobalRef(obj);
    g_oIpCam.m_objMdaInterface	= env->NewGlobalRef(cb);
#endif
    s_poCmdEnv = env;

	LOGI("Java_com_livecam_roadjun_Ipc_RegisterCallBack().");
    return 0;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    RegisterCallBack2
 * Signature: ()I
 */
 static JNIEnv *s_poMdaEnv = NULL;
jint 
Java_com_livecam_roadjun_Ipc_RegisterCallBack2(JNIEnv *env, jobject obj, jobject cb)
{ 
    g_oIpCam.m_pMdaEnv 			= env;
    g_oIpCam.m_objMdaCallBack 	= env->NewGlobalRef(obj);
    g_oIpCam.m_objMdaInterface	= env->NewGlobalRef(cb);
    s_poMdaEnv = env;

    //s_poEnv = env;

	LOGI("Java_com_livecam_roadjun_Ipc_RegisterCallBack2().");
    return 0;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    UnregisterCallBack
 * Signature: ()I
 */
jint 
Java_com_livecam_roadjun_Ipc_UnregisterCallBack(JNIEnv * env, jobject obj, jobject cb)
{
	LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack() begin...");
	g_oIpCam.StopService();
#if 1
	if (s_poCmdEnv)
	{
		//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) cmd DeleteGlobalRef(cb) begin...");
		//s_poCmdEnv->DeleteGlobalRef(cb);
		s_poCmdEnv->DeleteGlobalRef(g_oIpCam.m_objCmdCallBack);
		s_poCmdEnv->DeleteGlobalRef(g_oIpCam.m_objCmdInterface);
 		//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) cmd DeleteGlobalRef(cb) is ok.");
   //s_poCmdEnv->DeleteGlobalRef(obj);
  	//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) cmd DeleteGlobalRef(obj) is ok.");
   s_poCmdEnv = NULL;
	}
#else
	//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack() DeleteGlobalRef(cb) begin...");
	g_oIpCam.m_pCmdEnv->DeleteGlobalRef(cb);
	//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack() DeleteGlobalRef(cb) end.");
	g_oIpCam.m_pCmdEnv->DeleteGlobalRef(obj);
#endif
 	LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack()end.");
    return 0;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    UnregisterCallBack2
 * Signature: ()I
 */
jint 
Java_com_livecam_roadjun_Ipc_UnregisterCallBack2(JNIEnv * env, jobject obj, jobject cb)
{
	LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack2() begin...");
	
#if 1
	if (s_poMdaEnv)
	{
		//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) mda DeleteGlobalRef(cb) begin...");
		//s_poMdaEnv->DeleteGlobalRef(cb);
		s_poMdaEnv->DeleteGlobalRef(g_oIpCam.m_objMdaCallBack);
		s_poMdaEnv->DeleteGlobalRef(g_oIpCam.m_objMdaInterface);
 		//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) mda DeleteGlobalRef(cb) is ok.");
   //s_poMdaEnv->DeleteGlobalRef(obj);
  	//LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack(0) mda DeleteGlobalRef(obj) is ok.");
   s_poMdaEnv = NULL;
	}
#else
	g_oIpCam.m_pMdaEnv->DeleteGlobalRef(cb);
 	g_oIpCam.m_pMdaEnv->DeleteGlobalRef(obj);
#endif
 	LOGI("Java_com_livecam_roadjun_Ipc_UnregisterCallBack2()end.");
 	return 0;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    RealPlay
 * Signature: (I)I
 */
jint
Java_com_livecam_roadjun_Ipc_RealPlay(JNIEnv* env, jobject thiz, jint iStreamType)
{
	LOGI("Java_com_livecam_roadjun_Ipc_RealPlay(%d).", iStreamType);
	return g_oIpCam.RealPlay((BOOL)iStreamType);
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    StopPlay
 * Signature: ()I
 */
jint
Java_com_livecam_roadjun_Ipc_StopPlay(JNIEnv* env, jobject thiz)
{
	LOGI("Java_com_livecam_roadjun_Ipc_StopPlay().");
	return g_oIpCam.StopPlay();
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    OpenSound
 * Signature: ()I
 */
jint
Java_com_livecam_roadjun_Ipc_OpenSound(JNIEnv* env, jobject thiz)
{
	LOGI("com_livecam_roadjun_Ipc_OpenSound().");
	return g_oIpCam.OpenSound();
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    CloseSound
 * Signature: ()I
 */
jint
Java_com_livecam_roadjun_Ipc_CloseSound(JNIEnv* env, jobject thiz)
{
	LOGI("Java_com_livecam_roadjun_Ipc_CloseSound().");
	return g_oIpCam.CloseSound();
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    myPTZControl
 * Signature: (II)I
 */
jint
Java_com_livecam_roadjun_Ipc_myPTZControl(JNIEnv* env, jobject thiz, jint iCommand, jint iSpeed)
{
	LOGI("Java_com_livecam_roadjun_Ipc_myPTZControl(%d %d).", iCommand, iSpeed);
	return g_oIpCam.PTZControl(iCommand, iSpeed);
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    StartRecord
 * Signature: (L)I
 */
jint
Java_com_livecam_roadjun_Ipc_StartRecord(JNIEnv* env, jobject thiz, jstring strFileName)
{
	int			iRet;
	const char 	*pszFileName;
	
	pszFileName = env->GetStringUTFChars(strFileName, NULL);  
	if (pszFileName == NULL)
	{
		return -1;
	}

	iRet = g_oIpCam.StartRecord(pszFileName);
	
	env->ReleaseStringUTFChars(strFileName, pszFileName);
	
	return iRet;
}

//====================================================
/*
 * Class:     com_livecam_roadjun_Ipc
 * Method:    StopRecord
 * Signature: ()I
 */
jint
Java_com_livecam_roadjun_Ipc_StopRecord(JNIEnv* env, jobject thiz)
{
	LOGI("Java_com_livecam_roadjun_Ipc_StopRecord().");
	return g_oIpCam.StopRecord();
}

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
	LOGI("JNI_OnLoad().");
	g_oIpCam.m_poCmdJavaVM = vm;
	g_oIpCam.m_poMdaJavaVM = vm;
	
	return JNI_VERSION_1_4;
}

#ifdef __cplusplus
}
#endif

